package console

import (
	"fmt"
	"github.com/pterm/pterm"
	"os"
)

type (
	// Console wraps from the fmt.Sprintf,
	// 默认情况下，它实现了colorConsole来为控制台提供彩色输出
	Console interface {
		Success(format string, a ...interface{})
		Info(format string, a ...interface{})
		Debug(format string, a ...interface{})
		Warning(format string, a ...interface{})
		Error(format string, a ...interface{})
		Fatalln(format string, a ...interface{})
		MarkDone()
		Must(err error)
	}
	colorConsole struct {
	}
)

// NewConsole returns an instance of Console
func NewConsole() Console {
	return NewColorConsole()
}

// NewColorConsole returns an instance of colorConsole
func NewColorConsole() Console {
	pterm.EnableDebugMessages()
	return &colorConsole{}
}

func (c *colorConsole) Info(format string, a ...interface{}) {
	msg := fmt.Sprintf(format, a...)
	pterm.Info.Println(msg)
}

func (c *colorConsole) Debug(format string, a ...interface{}) {
	msg := fmt.Sprintf(format, a...)
	pterm.Debug.Println(msg)
}

func (c *colorConsole) Success(format string, a ...interface{}) {
	msg := fmt.Sprintf(format, a...)
	pterm.Success.Println(msg)
}

func (c *colorConsole) Warning(format string, a ...interface{}) {
	msg := fmt.Sprintf(format, a...)
	pterm.Warning.Println(msg)
}

func (c *colorConsole) Error(format string, a ...interface{}) {
	msg := fmt.Sprintf(format, a...)
	pterm.Error.Println(msg)
}

func (c *colorConsole) Fatalln(format string, a ...interface{}) {
	c.Error(format, a...)
	os.Exit(1)
}

func (c *colorConsole) MarkDone() {
	c.Success("Done.")
}

func (c *colorConsole) Must(err error) {
	if err != nil {
		c.Fatalln("%+v", err)
	}
}